package cn.itcast.usermanage.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@RequestMapping("user")
@Controller
public class UserController{

	@Autowired
	private UserService userService;
	
	/*@RequestMapping(value="users")
	public String toUsers(){
		
		return "users";
	}*/
	@RequestMapping(value="page/{pageName}")
	public String toAddUser(@PathVariable("pageName")String pageName){
		return pageName;
	}
	
	@RequestMapping(value="list")
	@ResponseBody
	public EasyUIResult queryUsersByPage(@RequestParam("page")Integer pageNum,@RequestParam("rows")Integer pageSize){
		
		return this.userService.queryEasyUIResult(pageNum, pageSize);	
	}
	/**
	 * 请求路径：/save
	 * 方法返回值：{status：200}
	 * 参数：User对象
	 */
	@RequestMapping(value="save")
	@ResponseBody
	public Map<String,Object> insertUser(User user){
		// 调用userService的新增方法
	      Boolean b = this.userService.insertUser(user);
		Map<String,Object> map = new HashMap<String,Object>();
		if(b){
			map.put("status","200");
		}else{
			map.put("status", "500");
		}
		return map;
	}
	/**
	 * 请求路径：/delete
	 * 方法返回值：{status：200}
	 * 参数：ids数组
	 */
	@RequestMapping(value="delete")
	@ResponseBody
	public Map<String,Object> deleteUser(@RequestParam("ids")List<Object>ids){
	  Map<String,Object> map = new HashMap<String,Object>();
	      boolean b = this.userService.deleteUser(ids);
	      if(b){
	    	  map.put("status", "200");
	      }else{
	    	  map.put("status", "500");
	      }
		return map;
	}
	@RequestMapping(value="export/excel")
	public String excel(Model model,@Param("page")Integer pageNum,@Param("rows")Integer pageSize){
		  EasyUIResult easyUIResult = this.userService.queryEasyUIResult(pageNum, pageSize);
		  model.addAttribute("sss", easyUIResult.getRows());
		    return "userExcelView";
	}
}
